package com.hspedu.hspliving.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.hspedu.hspliving.commodity.entity.AttrAttrgroupRelationEntity;
import com.hspedu.hspliving.commodity.entity.AttrEntity;
import com.hspedu.hspliving.commodity.service.AttrAttrgroupRelationService;
import com.hspedu.hspliving.commodity.service.AttrService;
import com.hspedu.hspliving.commodity.service.CategoryService;
import com.hspedu.hspliving.commodity.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hspedu.hspliving.commodity.entity.AttrgroupEntity;
import com.hspedu.hspliving.commodity.service.AttrgroupService;
import com.hspedu.common.utils.PageUtils;
import com.hspedu.common.utils.R;

import javax.annotation.Resource;


/**
 * 家居商品属性分组
 *
 * @author hsp
 * @email hsp@gmail.com
 * @date 2025-07-25 20:53:47
 */
@RestController
@RequestMapping("commodity/attrgroup")
public class AttrgroupController {
    @Autowired
    private AttrgroupService attrgroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("commodity:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrgroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表+条件查询
     */
    @RequestMapping("/list/{categoryId}")
//    @RequiresPermissions("commodity:attrgroup:list")
    public R listCategoryId(@RequestParam Map<String, Object> params,@PathVariable("categoryId") Long categoryId){
        PageUtils page = attrgroupService.queryPage(params,categoryId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("commodity:attrgroup:info")
    public R info(@PathVariable("id") Long id){
		AttrgroupEntity attrgroup = attrgroupService.getById(id);

        Long categoryId = attrgroup.getCategoryId();
        attrgroup.setCascadedCategoryId(categoryService.getCascadedCategoryId(categoryId));

        return R.ok().put("attrgroup", attrgroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("commodity:attrgroup:save")
    public R save(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.save(attrgroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("commodity:attrgroup:update")
    public R update(@RequestBody AttrgroupEntity attrgroup){
		attrgroupService.updateById(attrgroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("commodity:attrgroup:delete")
    public R delete(@RequestBody Long[] ids){
		attrgroupService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 获取属性组关联的基本属性
     */
    @RequestMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId){
        List<AttrEntity> attrs = attrService.getAttrByAttrGroupId(attrgroupId);

        return R.ok().put("data",attrs);
    }

    /**
     * 删除关联关系
     */
    @RequestMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrAttrgroupRelationEntity[] attrAttrgroupRelationEntities){
        attrService.deleteAttrRelationBatch(Arrays.asList(attrAttrgroupRelationEntities));
        return R.ok();
    }

//    @GetMapping("/{attrgroupId}/attr/allowrelation")
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrAllowRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params){
        PageUtils page = attrService.getAllowRelationAttr(params,attrgroupId);
        return R.ok().put("page",page);
    }


    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrAttrgroupRelationEntity> attrAttrgroupRelationEntities){
        //AttrAttrgroupRelationService 本身就提供批量保存的方法.
        attrAttrgroupRelationService. saveBatch(attrAttrgroupRelationEntities);
        return R.ok();
    }

    @RequestMapping("/{categoryId}/withattr")
    public R withAttr(@PathVariable("categoryId") Long categoryId){
        List<AttrGroupWithAttrsVo> vos = attrgroupService.getAttrGroupWithAttrsByCategoryId(categoryId);

        return R.ok().put("data",vos);
    }
}
